/**
 * Build config for electron renderer process
 */
import path from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
// @ts-ignore
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
// @ts-ignore
import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
// @ts-ignore
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import {merge} from 'webpack-merge';
import TerserPlugin from 'terser-webpack-plugin';
import baseConfig from './webpack.config.base';
import webpackPaths from './webpack.paths';
import checkNodeEnv from '../scripts/check-node-env';
import deleteSourceMaps from '../scripts/delete-source-maps';

checkNodeEnv('production');
deleteSourceMaps();

const devtoolsConfig =
    process.env.DEBUG_PROD === 'true'
        ? {
            devtool: 'source-map',
        }
        : {};

export default merge(baseConfig, {
    ...devtoolsConfig,

    mode: 'production',

    target: 'electron-renderer',

    entry: {
        core: 'core-js',
        runtime: 'regenerator-runtime/runtime',
        index: path.join(webpackPaths.srcRendererPath, 'index.tsx'),
        lrc: path.join(webpackPaths.srcRendererPath, 'pages/DesktopLyric/index.tsx'),
        update: path.join(webpackPaths.srcRendererPath, 'pages/Update/index.tsx')
    },

    output: {
        path: webpackPaths.distRendererPath,
        // @ts-ignore
        publicPath: './',
        filename: '[name].prod.js',
    },

    module: {
        rules: [
            {
                test: /\.s?(a|c)ss$/,
                // @ts-ignore
                use: [
                    MiniCssExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            sourceMap: true,
                            importLoaders: 1,
                        },
                    },
                    'css-loader',
                    'sass-loader',
                ],
                include: /\.module\.s?(c|a)ss$/,
            },
            {
                test: /\.less$/,
                // @ts-ignore
                use: [{
                    loader: "style-loader" // creates style nodes from JS strings
                }, {
                    loader: "css-loader" // translates CSS into CommonJS
                }, {
                    loader: "less-loader", // compiles Less to CSS
                    options: {
                        lessOptions: {
                            javascriptEnabled: true,
                        }
                    }
                }]
            },
            {
                test: /\.s?(a|c)ss$/,
                // @ts-ignore
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
                exclude: /\.module\.s?(c|a)ss$/,
            },
            //Font Loader
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/i,
                // @ts-ignore
                type: 'asset/resource',
            },
            // SVG Font
            {
                test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
                use: {
                    loader: 'url-loader',
                    // @ts-ignore
                    options: {
                        limit: 10000,
                        mimetype: 'image/svg+xml',
                    },
                },
            },
            // Common Image Formats
            {
                test: /\.(?:ico|gif|png|jpg|jpeg|webp)$/,
                // @ts-ignore
                use: 'url-loader',
            },
        ],
    },

    optimization: {
        minimize: true,
        minimizer: [
            new TerserPlugin({
                parallel: true,
            }),
            new CssMinimizerPlugin(),
        ],
    },

    plugins: [
        /**
         * Create global constants which can be configured at compile time.
         *
         * Useful for allowing different behaviour between development builds and
         * release builds
         *
         * NODE_ENV should be production so that modules do not perform certain
         * development checks
         */
        new webpack.EnvironmentPlugin({
            NODE_ENV: 'production',
            DEBUG_PROD: false,
        }),

        new MiniCssExtractPlugin({
            filename: 'style.css',
        }),

        new BundleAnalyzerPlugin({
            analyzerMode:
                process.env.OPEN_ANALYZER === 'true' ? 'server' : 'disabled',
            openAnalyzer: process.env.OPEN_ANALYZER === 'true',
        }),

        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: path.join(webpackPaths.srcRendererPath, 'index.ejs'),
            minify: {
                collapseWhitespace: true,
                removeAttributeQuotes: true,
                removeComments: true,
            },
            isBrowser: false,
            isDevelopment: process.env.NODE_ENV !== 'production',
            excludeChunks: ['lrc', 'update']
        }),
        new HtmlWebpackPlugin({
            filename: 'desktop-lyric.html',
            template: path.join(webpackPaths.srcRendererPath, 'pages/DesktopLyric/index.html'),
            minify: {
                collapseWhitespace: true,
                removeAttributeQuotes: true,
                removeComments: true,
            },
            isBrowser: false,
            isDevelopment: process.env.NODE_ENV !== 'production',
            excludeChunks: ['index', 'update']
        }),
        new HtmlWebpackPlugin({
            filename: 'update.html',
            template: path.join(webpackPaths.srcRendererPath, 'pages/Update/index.html'),
            minify: {
                collapseWhitespace: true,
                removeAttributeQuotes: true,
                removeComments: true,
            },
            isBrowser: false,
            isDevelopment: process.env.NODE_ENV !== 'production',
            excludeChunks: ['index', 'lrc']
        }),
    ],
});
